package com.geekstudy.week4.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class DynamicProxyAdapter {

    final Logger logger = LoggerFactory.getLogger(DynamicProxyAdapter.class);

    private DynamicProxyInvocationContext dynamicProxyInvocationContext;

    public DynamicProxyAdapter(DynamicProxyInvocationContext dynamicProxyInvocationContext) {
        this.dynamicProxyInvocationContext = dynamicProxyInvocationContext;
    }


    public Object invoke(Class c, Object proxy, Method method, Object[] args) throws Throwable {
        Object target = dynamicProxyInvocationContext.getTarget(c);

        InvocationHandler invocationHandler = new InvocationHandler()  {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(target, args);
            }
        };
        Object result = invocationHandler.invoke(proxy, method, args);
        return result;
    }
}
